package org.marsdonne.sync;

import com.fasterxml.jackson.databind.JsonNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

public interface Synced {
    Logger logger = LoggerFactory.getLogger(Synced.class);

    void created(Date dateSynced, JsonNode content, long version);

    void changed(Date dateSynced, JsonNode content, long version);

    void deleted(Date dateSynced, JsonNode content, long version);

    default void sync(Event event) {
        try {
            switch (event.getAction()) {
                case CHANGED:
                    changed(event.getDateSynced(), event.getContent(), event.getVersion());
                    break;
                case CREATED:
                    created(event.getDateSynced(), event.getContent(), event.getVersion());
                    break;
                case DELETED:
                    deleted(event.getDateSynced(), event.getContent(), event.getVersion());
                    break;
            }
        } catch (Exception ex) {
            logger.warn(ex.getMessage(), ex);
        }
    }
}
